package com.harvey.exampreparation.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.harvey.exampreparation.R;

/**
 * 基础控件实例
 *
 * 代码阅读顺序：
 *   res/layout/activity_base_view.xml      布局实现
 *   activity/BaseViewActivity（本类）        控件监听器实现
 */
public class BaseViewActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_view);

        button();

        radioButton();

        checkBox();

        alertDialog();
    }

    /**
     * 按钮监听时间
     * （匿名内部类实现）
     */
    private void button() {
        Button button = findViewById(R.id.btn_example);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BaseViewActivity.this, "你点击了按钮", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 单选框监听事件
     * （匿名内部类实现）
     */
    private void radioButton() {
        RadioGroup radioGroup = findViewById(R.id.rg_example);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = findViewById(checkedId);
                Toast.makeText(BaseViewActivity.this, "你选中了" + rb.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 复选框监听事件
     * （实现接口）
     */
    private void checkBox() {
        CheckBox selection1 = findViewById(R.id.cb_example_selection1);
        CheckBox selection2 = findViewById(R.id.cb_example_selection2);
        CheckBox selection3 = findViewById(R.id.cb_example_selection3);
        CheckBox selection4 = findViewById(R.id.cb_example_selection4);

        selection1.setOnCheckedChangeListener(this);
        selection2.setOnCheckedChangeListener(this);
        selection3.setOnCheckedChangeListener(this);
        selection4.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Toast.makeText(BaseViewActivity.this, "你选择了" + buttonView.getText(), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(BaseViewActivity.this, "你取消选择了" + buttonView.getText(), Toast.LENGTH_SHORT).show();

        }
    }

    /**
     * 打开对话框
     */
    private void alertDialog() {
        Button norm = findViewById(R.id.btn_example_norm_dialog);

        norm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(BaseViewActivity.this)
                        .setTitle("删除")                            // 设置对话框标题
                        .setMessage("是否删除此项？")                  // 设置对话框显示的信息
                        // 设置确定按钮的文本及监听器
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(BaseViewActivity.this, "你删除了一项内容", Toast.LENGTH_SHORT).show();
                            }
                        })
                        // 设置否定按钮的文本及监听器
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(BaseViewActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
                            }
                        })
                        // 设置中立按钮的文本及监听器
                        .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(BaseViewActivity.this, "你点击了忽略", Toast.LENGTH_SHORT).show();
                            }
                        }).create().show();
            }
        });

        Button radio = findViewById(R.id.btn_example_radio_dialog);

        final String[] fruits = {"西瓜", "苹果", "雪梨", "芒果", "橙子"};

        radio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(BaseViewActivity.this)
                        .setTitle("选择最喜欢的水果")                            // 设置对话框标题
                        // 设置单选按钮的选项数组, 选中项索引, 以及监听器
                        .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(BaseViewActivity.this, "你选择了" + fruits[which], Toast.LENGTH_SHORT).show();
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();
            }
        });
    }
}